<template>
    <div class='upload-file'>
        <el-upload
          action="1"
          accept=".xlsx, .xls"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="fileChange">
            <el-button type="success">读取考勤</el-button>
        </el-upload>
        <!-- <el-button type="primary" plan>清除缓存</el-button> -->
    </div>
</template>

<script setup lang="ts">
import { diffHours, getDaysInMonth } from '@/utils/tools'
import { readExcel } from '@/utils/readExcel'
// 人员信息
interface Person {
    // 姓名
    name:string,
    // 工作地址
    workAddress:string,
    // 部门
    department:string,
    // 工作岗位
    position:string,
    // 工号
    workNum:string,

    normal: number,           // 正常
    fieldWork: number,        // 外勤
    rest: number,             // 休息
    late: number,             // 迟到
    personalLeave: number,    // 事假

    // 缺卡统计
    missing: {
        clockIn: number,        // 上班缺卡
        clockOut: number,       // 下班缺卡
        both: number            // 上下班缺卡
    },

    // 外勤细分
    fieldWorkDetail: {
        morning: number,        // 上班外勤
        afternoon: number,      // 下班外勤
        allDay: number          // 上下班外勤
    },

    // 严重缺勤
    absence: number           // 旷工

}

const emits = defineEmits(['submit'])
const fileChange = (e:any)=>{
    if(!e.raw) return
    readExcel(e.raw, (data:any)=>{
      setData(data.json[0] as string[][])
    }, { sheetNames: ['月度汇总'] })
}
// 数据回填
const setData = (
    data: string[][]
)=>{
    // 获取考勤时间
    const time = data[0][0].slice(10,17).split('-')
    // 展示时间范围
	const timeRange = data[0][0].slice(10).split('至')
    // 本月一共多少天
    const allDays = getDaysInMonth(time[0], time[1])

    // 创建考勤计算函数

    // 人员信息数组
    const employeeWorkList = data.map((item,index)=>{
        if(index < 4) return
        const calcStatusNum = calcStatusNumFns(item.slice(35))
        return {
            name: item[0],
            workAddress: item[1],
            department: item[2],
            position: item[4],
            workNum: item[5],
            attendanceDays: calcStatusNum["出勤天数"],
            normal: (()=>{
                const a = calcStatusNum["正常"]
                return a
            })(),
            rest: calcStatusNum["休息"],
            late: calcStatusNum["迟到"],
            personalLeave: calcStatusNum["事假"],
            // 缺卡统计
            missing: {
                clockIn: calcStatusNum["上班缺卡"],// 上班缺卡
                clockOut: calcStatusNum["下班缺卡"],
                both: calcStatusNum['上班缺卡,下班缺卡']
            },
            fieldWorkDetail: {
                morning: calcStatusNum["上班外勤"],
                afternoon: calcStatusNum["下班外勤"],
                allDay: calcStatusNum["上班外勤,下班外勤"],
            },
        }
    }).filter(Boolean)
    emits('submit',employeeWorkList as Person[] ,timeRange)
}
const calcStatusNumFns = (itemArr:string[]) => {
    const calcNum = (str:string) => itemArr.filter(i => i.includes(str)).length
    // const calcRegexNum = (regex:any) => itemArr.filter(i => regex.test(i)).length
    return {
        '出勤天数': (()=>{
            return itemArr.reduce((pre, cur)=>{
                if(/^(?=.*事假)(?=.*正常).+$/.test(cur)) {
                    pre+ 0.5
                }else if(/正常|缺卡|出差|外勤|迟到|休息并打卡/.test(cur)){
                    pre++
                }
                return pre
            },0)
        })(),
        '正常': calcNum('正常'),
        '休息': calcNum('休息'),
        '迟到': calcNum('迟到'),
        '事假': calcNum('事假'),
        '出差': calcNum('出差'),
        '上班缺卡': calcNum('上班缺卡'),
        '下班缺卡': calcNum('下班缺卡'),
        '上班缺卡,下班缺卡': calcNum('上班缺卡,下班缺卡'),
        '上班外勤': calcNum('上班外勤'),
        '下班外勤': calcNum('下班外勤'),
        '上班外勤,下班外勤': calcNum('上班外勤,下班外勤'),
    }
}

</script>